package com.atguigu.gulimall.gateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;

/**
 * @Classname CorsConfiguration
 * @Description TODO
 * @Date 2024/6/11 16:13
 * @Created by 369502594
 */
@Configuration
public class GulimallCorsConfiguration {
    @Bean
    public CorsWebFilter corsWebFilter(){

        // 1、跨域配置源
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        // 2、跨域规则
        CorsConfiguration corsConfiguration = new CorsConfiguration();

        // 1、配置跨域
        corsConfiguration.addAllowedHeader("*");    //允许所有请求头
        corsConfiguration.addAllowedMethod("*");    //允许所有请求方法
        corsConfiguration.addAllowedOriginPattern("*"); //允许所有来源的跨域请求
        corsConfiguration.setAllowCredentials(true);    //允许携带cookie跨域
        source.registerCorsConfiguration("/**",corsConfiguration);
        return new CorsWebFilter(source);
    }
}
